package cn;

import java.util.*;
import java.sql.*;
import java.io.*;

public class MysqlTest1  {
    public static void main(String args[]) {   
    	String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        Connection con;
        String sql;
        Statement stmt;
        int num;
        String name,sex;
        int age;
		
       try {
          Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");
       } catch(java.lang.ClassNotFoundException e) {
           System.err.print("ClassNotFoundException: ");
           System.err.println(e.getMessage());
       }
        try {	    
            con = DriverManager.getConnection(url, "system", "system");
            stmt = con.createStatement();            
            //向student表中插入一行录记录            
            Scanner in=new Scanner(System.in);
            System.out.print("\n输入学生的自然信息");
            System.out.print("\n-----------------------------");
            System.out.print("\n学号：");
            num=in.nextInt();
             in.nextLine();     
             System.out.print(" 姓名：");
             name=in.nextLine();             
             System.out.print(" 性别：");
             sex=in.nextLine();
             System.out.print("年龄：");
             age=in.nextInt();

        	sql = "INSERT INTO STUDENT " +
                            "VALUES('"+num+"'"+","+"'"+name+"'"+",'"+sex+"',"+age+")";
            
             //执行插入的sql语句
        	stmt.executeUpdate(sql);
        	
     	//检索student表中的所有记录并获取数据输出。
        	sql = " SELECT * FROM STUDENT";         
        	ResultSet rs = stmt.executeQuery(sql);
        	
        	System.out.println("\n\n\n\n");
        	System.out.println("全体学生信息表");
        	System.out.println("-------------------------");
        	System.out.println("学号    姓名    性别  年龄");  
            System.out.println("-------------------------");
            //取出结果集rs中的所有记录
            while(rs.next()){
        	     num = rs.getInt(1); //取出当前行第1列数据
        	     name = rs.getString(2); //取出当前行第2列数据
        	     sex = rs.getString(3);
        	     age = rs.getInt(4);        		 
        	     System.out.println(num+"    "+name+"   "+sex+"   "+age);
        	}       
                System.out.println("-------------------------");
        	//关闭连接。
        	stmt.close();
        	con.close();
     	} catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }          
    }
}


